package test00;

import test00.Student;

import java.lang.reflect.Array;
import java.lang.reflect.Field;

/**
 * @Author lzp
 * @Date 2024-03-27
 */
public class test07 {
    public static void main(String[] args) throws Exception {

        // 第一：创建数组 - 学生
        Object arr = Array.newInstance(Student.class, 2);

        // 第二：实例化学生对象
        Class<Student> clazz = Student.class;

        // 1.实例化第一个学生对象
        Student stu1 = clazz.getConstructor().newInstance();

        Field nameFiled = clazz.getDeclaredField("name");
        Field ageFiled = clazz.getDeclaredField("age");
        Field scoreFiled = clazz.getDeclaredField("score");

        nameFiled.setAccessible(true);
        ageFiled.setAccessible(true);
        scoreFiled.setAccessible(true);

        nameFiled.set(stu1, "张三");
        ageFiled.set(stu1, 18);
        scoreFiled.set(stu1, 100D);

        // 实例化第二个学生
        Student stu2 = clazz.getConstructor(String.class, int.class, Double.class).newInstance("李四", 20, 99.9);

        // 把学生，添加到数组
        Array.set(arr, 0, stu1);
        Array.set(arr, 1, stu2);

        Object s1 = Array.get(arr, 0);
        Object s2 = Array.get(arr, 1);

        System.out.println(s1);
        System.out.println(s2);

    }
}
